﻿using System;
using System.Windows.Data;
using System.Windows.Media;

namespace Notoric.ViewModel.Converters
{
    /// <summary>
    /// Converts the <see cref="Notoric.Model.Elements.Brush"/> instances to <see cref="System.Windows.Media.Brush"/> and back.
    /// </summary>
    [ValueConversion(typeof(Notoric.Model.Elements.Brush), typeof(System.Windows.Media.Brush))]
    public class BrushConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>A converted value. If the method returns <see langword="null"/>, the valid null value is used.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;

            Brush retBrush = null;

            if (value.GetType() == typeof(Model.Elements.SolidColorBrush))
            {
                var brush = (Model.Elements.SolidColorBrush)value;

                Color color = new Color()
                {
                    R = brush.Color.Red,
                    G = brush.Color.Green,
                    B = brush.Color.Blue,
                    A = brush.Color.Alpha
                };

                retBrush = new SolidColorBrush(color);
            }

            return retBrush;
        }

        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>A converted value. If the method returns <see langword="null"/>, the valid null value is used.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;

            Model.Elements.Brush retBrush = null;

            if (value.GetType() == typeof(SolidColorBrush))
            {
                var brush = (SolidColorBrush)value;
                var color = new Model.Elements.Color()
                {
                    Red = brush.Color.R,
                    Green = brush.Color.G,
                    Blue = brush.Color.B,
                    Alpha = brush.Color.A
                };

                retBrush = new Model.Elements.SolidColorBrush(color);
            }

            return retBrush;
        }

        #endregion
    }
}
